package org.bing.engine.utility.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;

import org.bing.engine.common.logging.Log;
import org.bing.engine.common.logging.LogFactory;


public class FixedQueue<T> {
	protected static final Log logger = LogFactory.getLog(FixedQueue.class);
	private final LinkedBlockingQueue<T> queue;
	private int capacity;

	public FixedQueue(int size) {
		this.capacity = size;
		queue = new LinkedBlockingQueue<T>(capacity);
	}

	public boolean offer(T target) {
		boolean flag = queue.offer(target);
		while (!flag) {
			queue.poll();
			flag = queue.offer(target);
		}
		return flag;
	}

	public T poll() {
		return queue.poll();
	}

	public boolean remove(T target) {
		return queue.remove(target);
	}

	public List<T> list() {
		List<T> list = new ArrayList<T>(queue);
		return list;
	}

	public int size() {
		return queue.size();
	}

	public int capacity() {
		return capacity;
	}

	public void clear() {
		queue.clear();
	}
}
